
#include <stdio.h>


#include <cv.h>
#include <highgui.h>
#include <ml.h>
#include "resourceTracker.h"


#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"


/*
 *  GestureClassifier_scrap.cpp
 *  
 *
 *  Created by Rasmus Kyng on 11/01/2011.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */


//class GestureClassifier {
//public:
//	train( CvDTreeTrainData*
//
//}

using namespace cv;

int main(int argc, const char* argv[]) {
	
	const char* videoNameString;
	
	if (argc == 1) {
		videoNameString = "locVid1c";
	} else {
		videoNameString = argv[1];
	}
	
	//Trying convexity defect first
	//CvBoost* boostClassifier = new CvBoost();
	
	ResourceTracker* rt = new ResourceTracker( "../config/runtimeSettings/cfg.xml" );
	rt->loadCategory("TestData");
	
	cv::Ptr< cv::VideoCapture > capPtr;
	cv::Ptr< VideoPointTagSequence > seqPtr;
	

	
	capPtr = rt->getFileVideoCapture( videoNameString );
	seqPtr = rt->getPointTags( videoNameString );
	
	int handRectSideLength = 200;
	
	Rect handRect = Rect( 0, 0, handRectSideLength, handRectSideLength);
	
	int tagCount = seqPtr->tagCount;
	
	// -- visualisation: set up -- //
	
	char* mainWindowName = "classifier scrap";
	char* handFrameWindowName = "hand rect";
	char* pmsfWindowName = "pmsf";
	char* floodFillWindowName = "flood filled";
	char* contourWindowName = "contours";
	
	namedWindow( mainWindowName, 1 );
	namedWindow( handFrameWindowName, 1 );
	namedWindow( pmsfWindowName, 1 );
	namedWindow( floodFillWindowName, 1 );
	namedWindow( contourWindowName, 1 );
	
	//Note: using OpenCV/C function as this has not been implemented in OpenCV/C++
	
	int ybase = 100;
	int yskip = handRectSideLength + 50;
	
	cvMoveWindow( mainWindowName, 0, ybase);
	cvMoveWindow( handFrameWindowName, 700, ybase );
	cvMoveWindow( pmsfWindowName, 700, ybase + 1*yskip );
	cvMoveWindow( floodFillWindowName, 700, ybase + 2*yskip );
	cvMoveWindow( contourWindowName, 700, ybase + 3*yskip );
	
	Mat frame;
	
	for( int frameCount = 0; frameCount<tagCount; frameCount++) {
		
		(*capPtr) >> frame;
		
		// -- tracking frame -- //
		Point groundTruth = seqPtr->getNextPointTag();
		handRect = Rect( groundTruth.x - handRect.width/2, groundTruth.y - handRect.height/2, handRect.width, handRect.height );
		
		//forcing rectangle to stay inside frame
		if ( handRect.x < 0 ) handRect.x = 0;
		if ( frame.cols < handRect.x + handRect.width ) handRect.x = frame.cols - handRect.width;
		if ( handRect.y < 0 ) handRect.y = 0;
		if ( frame.rows < handRect.y + handRect.height ) handRect.y = frame.rows - handRect.height;
		
		
		// -- segmentation on hand ROI -- //
		
		Mat handImg(frame, handRect);
		
		Mat handPMSFImg;

		int spatialRad = 10;
		int colorRad = 10;
		int maxPyrLevel = 1;
		
		pyrMeanShiftFiltering( handImg, handPMSFImg, spatialRad, colorRad, maxPyrLevel );
		
//		dilate( handEdgeImg,handEdgeImg, Mat(), Point(-1, -1), 1);
//		
//		vector<vector<Point> > contours;
//		vector<Vec4i> hierarchy;
//		
//		findContours( handEdgeImg, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
//		
//		Mat contoursImg = Mat::zeros(handEdgeImg.size(), CV_8UC3);
		
		//
		
		Mat floodFilled;
		
		Mat mask( handPMSFImg.rows+2, handPMSFImg.cols+2, CV_8UC1, Scalar::all(0) );
		
		handPMSFImg.copyTo( floodFilled );
		
		Scalar colourDiff = Scalar::all(5);
		
		floodFill( floodFilled, mask, Point(groundTruth.x-handRect.x, groundTruth.y-handRect.y),
											Scalar::all(255), 0, colourDiff, colourDiff );
		
			
		// -- visualisation: display -- //
		
		{
			rectangle( frame, Point(handRect.x, handRect.y),
					  Point(handRect.x + handRect.width, handRect.y + handRect.height),
					  Scalar::all(250));
			
			circle( frame, groundTruth, 1, Scalar(0, 255, 0), 2 ); //radius 1, thickness 2, colour green
			//NOTE: BGR colour!
			
			
//			if( contours.size() > 0 )
//			{
//				// iterate through all the top-level contours,
//				// draw each connected component with its own random color
//				int idx = 0;
//				for( ; idx >= 0; idx = hierarchy[idx][0] )
//				{
//					Scalar color( (rand()&255)+10, (rand()&255)+10, (rand()&255)+10 );
//					drawContours( contoursImg, contours, idx, color, CV_FILLED, 8, hierarchy );
//				}
//			}
			
			
			imshow( mainWindowName, frame );
			imshow( handFrameWindowName, handImg );
			imshow( pmsfWindowName, handPMSFImg );
			imshow( floodFillWindowName, floodFilled );
			//imshow( contourWindowName, contoursImg );
			

			
			
		}
		
		// -- get next frame -- //
		
		waitKey(1);
	}
	

	
	delete rt;
	
	//delete boostClassifier;

	return 0;

}